/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;


/**
 * Simple class to deal with Text files
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class ReadWriteTextFiles {
  
  public static String getContents(String fileName) {
    return getContents(fileName, "UTF-8");
  }

  /**
   * Fetch the entire contents of a text file, and return it in a String.
   * This style of implementation does not throw Exceptions to the caller.
   *
   * @param fileName the file name
   * @return the contents
   */
  public static String getContents(String fileName, String encode) {
    StringBuffer fileData = new StringBuffer(1048576);
    BufferedReader reader;
    try {
      Reader fileReader = null;
      if (encode.length() > 0) {
        fileReader = new InputStreamReader(new FileInputStream(fileName), 
          encode);
      } else {
        fileReader = new InputStreamReader(new FileInputStream(fileName));        
      }
      reader = new BufferedReader(fileReader);
      char[] buf = new char[1048576];
      int numRead = 0;
      while ((numRead = reader.read(buf)) != -1){
        fileData.append(buf, 0, numRead);
      }
      reader.close();
      return fileData.toString();
    } catch (Exception e) {
      DumpExceptions.dumpException(e);
    }

    return "";
  }

  /**
   * Change the contents of text file in its entirety, overwriting any
   * existing text.
   * 
   * This style of implementation throws all exceptions to the caller.
   *
   * @param aFile is an existing file which can be written to.
   * @param aContents the a contents
   * @throws FileNotFoundException if the file does not exist.
   * @throws IOException if problem encountered during write.
   */
  public static void setContents(File aFile, String aContents)
  throws FileNotFoundException, IOException {    
    //use buffering
    Writer output = new BufferedWriter(new FileWriter(aFile));
    try {
      //FileWriter always assumes default encoding is OK!
      output.write(aContents);
    } finally {
      output.close();
    }
  }
}
